package net.uhuijia.zb.userzhubolive.service.impl;

import net.uhuijia.appbase.model.Page;
import net.uhuijia.constants.DBConst;
import net.uhuijia.enums.ErrorCodeEnum;
import net.uhuijia.enums.LiveState;
import net.uhuijia.exception.BusinessException;
import net.uhuijia.goods.goodspu.clientservice.GoodSpuClientService;
import net.uhuijia.goods.goodspu.persistence.model.GoodSpu;
import net.uhuijia.user.usermall.persistence.model.UserMall;
import net.uhuijia.utils.IticConst;
import net.uhuijia.vo.zb.UserZhuboListVo;
import net.uhuijia.vo.zb.ZbDetailVo;
import net.uhuijia.zb.userzhubo.persistence.model.UserZhubo;
import net.uhuijia.zb.userzhubo.service.UserZhuboService;
import net.uhuijia.zb.userzhubolive.service.UserZhuboLiveService;
import net.uhuijia.zb.userzhubolivegood.persistence.model.UserZhuboLiveGood;
import net.uhuijia.zb.userzhubolivegood.persistence.model.UserZhuboLiveGoodExample;
import net.uhuijia.zb.userzhubolivegood.service.UserZhuboLiveGoodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.zb.userzhubolive.persistence.dao.UserZhuboLiveDao;
import net.uhuijia.zb.userzhubolive.persistence.model.UserZhuboLive;
import net.uhuijia.zb.userzhubolive.persistence.model.UserZhuboLiveExample;
import net.uhuijia.zb.userzhubolive.persistence.model.UserZhuboLiveExample.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class UserZhuboLiveServiceImpl
     extends CrudService<UserZhuboLiveDao,UserZhuboLive,UserZhuboLiveExample> implements UserZhuboLiveService{
    @Autowired
    private UserZhuboLiveDao userZhuboLiveDao;
    @Autowired
    private UserZhuboService userZhuboService;
    @Autowired
    private UserZhuboLiveGoodService userZhuboLiveGoodService;
    @Autowired
    private GoodSpuClientService goodSpuClientService;


    /**
     * 分页查询直播列表
     * @param example
     * @return
     */
    @Override
    public List<UserZhuboLive> findUserZhuBoLiveByPage(UserZhuboLiveExample example) {
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andLiveStateEqualTo(LiveState.Living.getState());
        example.setOrderByClause(" create_time desc");
        List<UserZhuboLive> userZhuboLives =  this.list(example);
        return userZhuboLives;
    }
    /**
     * 查询主播直播列表数量
     * @param example
     * @return
     */
    @Override
    public int findUserZhuBoLiveCount(UserZhuboLiveExample example) {
        Criteria criteria = example.createCriteria();
        criteria.andIsEffectiveEqualTo(DBConst.TRUE);
        criteria.andLiveStateEqualTo(LiveState.Living.getState());
        Integer count = this.count(example);
        return count ==null?0:count.intValue();
    }


    /**
     * 将直播列表转换成Vo
     * @param userZhuboLive
     * @return
     */
    @Override
    public UserZhuboListVo userZhuboLiveWrapUserZhuboListVo(UserZhuboLive userZhuboLive){
        UserZhuboListVo userZhuboListVo = new UserZhuboListVo();
        userZhuboListVo.setName(userZhuboLive.getNickName());
        userZhuboListVo.setTitle(userZhuboLive.getTitle());
        userZhuboListVo.setFabulousCount(userZhuboLive.getFabulousCount()==null?0:userZhuboLive.getFabulousCount());
        userZhuboListVo.setLiveState(userZhuboLive.getLiveState());
        userZhuboListVo.setGoodCount(userZhuboLive.getGoodCount()==null?0:userZhuboLive.getGoodCount());
        userZhuboListVo.setOnlineCount(userZhuboLive.getOnlineCount()==null?0:userZhuboLive.getOnlineCount());
        //商品图片
        UserZhuboLiveGoodExample example = new UserZhuboLiveGoodExample();
        Page page = new Page();
        page.setRecordNow(0);
        page.setPageSize(10);
        example.setPage(page);
        List<UserZhuboLiveGood> userZhuboLiveGoods = this.userZhuboLiveGoodService.findUserZhuboLiveGoodByLiveIdAndPage(userZhuboLive.getId(),example);
        List<String> goodImages = new ArrayList<>();
        for(UserZhuboLiveGood userZhuboLiveGood :userZhuboLiveGoods){
            GoodSpu goodSpu = this.goodSpuClientService.getGoodSpuBySpuId(userZhuboLiveGood.getGoodId());
            if(goodSpu!=null){
                goodImages.add(goodSpu.getMainSkuImage());
            }else
                continue;
        }
        userZhuboListVo.setGoodImages(goodImages);
        //封面图
        userZhuboListVo.setCoverImage(userZhuboLive.getCoverImage());
        userZhuboListVo.setZbUserId(userZhuboLive.getUserId());
        userZhuboListVo.setLiveId(userZhuboLive.getId());
        return userZhuboListVo;
    }
    /**
     * 查询用户正在直播的信息
     * @param userId
     * @return
     */
    @Override
    public UserZhuboLive getUserZhuboOnlineByUserId(String userId) {
        if(StringUtils.isEmpty(userId))
            throw  new IllegalArgumentException();
        UserZhuboLive userZhuboLive = this.userZhuboLiveDao.getUserZhuboOnlineByUserId(userId);
        return userZhuboLive;
    }


    @Override
    public ZbDetailVo zbOnlineDetail(UserMall userMall,String liveId) {
        UserZhuboLive userZhuboLive = this.get(liveId);
        if(userZhuboLive!=null){
            ZbDetailVo detailVo = new ZbDetailVo();
            //查询主播信息
            UserZhubo userZhubo = this.userZhuboService.getUserZhuboByUserId(userMall.getId());
            String title = userZhuboLive.getTitle();
            String headImage = userZhubo.getZbHeadImg();
            String nickName = userZhubo.getNickName();
            detailVo.setTitle(title);
            detailVo.setFabulousCount(userZhuboLive.getFabulousCount());
            detailVo.setHeadImage(headImage);
            detailVo.setNickName(nickName);
            detailVo.setZbUserId(userMall.getId());
            detailVo.setLiveId(liveId);
            detailVo.setPushUrl(userZhubo.getZbPushUrl());
            detailVo.setPullFlvUrl(userZhubo.getZbPullFlvUrl());
            detailVo.setPullHlsUrl(userZhubo.getZbPullHlsUrl());
            detailVo.setPullRtmpUrl(userZhubo.getZbPullRtmpUrl());
            //TODO 是否关注主播
            return detailVo;
        }else{
            throw  new BusinessException(ErrorCodeEnum.zb_not_online);
        }
    }
}